/**
 * 初始化 Tree Table 的封装
 *
 * @author cyf
 */
(function () {
    var BSTreeTable = function (bstableId, url, columns) {
        this.btInstance = null;					//jquery和bootstrapTreeTable绑定的对象
        this.bstableId = bstableId;
        this.url = url;
        this.method = "post";
        this.columns = columns;
        this.data = {};// ajax的参数
        this.expandColumn = null;// 展开显示的列 
        this.id = 'id';// 选取记录返回的值
        this.parentId = 'parentId';// 用于设置父子关系
        this.expandAll = false;// 是否默认全部展开
        this.expandFirst = true;// 是否默认第一级展开--expandAll为false时生效
        this.showColumns = true;
        this.showRefresh = true;
        this.rootIdValue = null;
        this.toolbarId = bstableId + "Toolbar";
        this.height = 0;						//默认表格高度665
        this.subHeight = 0;
    };

    BSTreeTable.prototype = {
        /**
         * 初始化bootstrap table
         */
        init: function () {
            if(this.height==0){
                this.autoHeight();
            }
            var tableId = this.bstableId;
            this.btInstance =
                $('#'+tableId).bootstrapTreeTable({
                    id: this.id,// 选取记录返回的值
                    parentId: this.parentId,// 用于设置父子关系
                    rootIdValue: this.rootIdValue,//设置根节点code值----可指定根节点，默认为null,"",0,"0"
                    type: this.method, //请求数据的ajax类型
                    url: this.url,   //请求数据的ajax的url
                    ajaxParams: this.data, //请求数据的ajax的data属性
                    expandColumn: this.expandColumn,//在哪一列上面显示展开按钮,从0开始
                    striped: false,   //是否各行渐变色
                    expandAll: this.expandAll,  //是否全部展开
                    expandFirst: this.expandFirst,  //是否默认第一级展开--expandAll为false时生效
                    columns: this.columns,		//列数组
                    toolbar: "#" + this.toolbarId,//顶部工具条
                    height: this.height,
                    showColumns: this.showColumns,     		//是否显示所有的列
                    showRefresh: this.showRefresh,     		//是否显示刷新按钮
                    onLoadSuccess: function(){  //加载成功时执行
                        var elements =$('#' + tableId+" > tbody").find(".btnMore");
                        elements.each(function() {
                            if ($(this).attr("data-mouseover-binded") == undefined) {
                                $(this).attr("data-mouseover-binded", true);
                                var timeoutHover;
                                elements.mouseover(function() {
                                    var more = $(this),
                                        items = more.next(),
                                        pos = more.position(),
                                        top = pos.top - items.height() / 2 + 9,
                                        left = pos.left - items.width() - 15;
                                    if (!items.hasClass("moreItems")) {
                                        return
                                    }
                                    $('#' + tableId+" > tbody").find(".btnMore.open").removeClass("open").find("i").removeClass("fa-chevron-circle-left").addClass("fa-chevron-circle-right");
                                    $('#' + tableId+" > tbody").find(".moreItems.open").removeClass("open").hide();
                                    more.addClass("open").find("i").removeClass("fa-chevron-circle-right").addClass("fa-chevron-circle-left");
                                    items.addClass("open").css({
                                        top: top,
                                        left: left
                                    }).show();
                                    if (items.attr("data-hover-binded") == undefined) {
                                        items.attr("data-hover-binded", true);
                                        items.hover(function() {
                                            window.clearTimeout(timeoutHover)
                                        }, function() {
                                            window.clearTimeout(timeoutHover);
                                            timeoutHover = window.setTimeout(function() {
                                                more.removeClass("open").find("i").removeClass("fa-chevron-circle-left").addClass("fa-chevron-circle-right");
                                                items.hide()
                                            }, 500)
                                        }).find("a").click(function() {
                                            more.removeClass("open").find("i").removeClass("fa-chevron-circle-left").addClass("fa-chevron-circle-right");
                                            items.hide()
                                        })
                                    }
                                })
                            }
                        });
                    }
                });
            return this;
        },
        autoHeight:function(){
            var t_top = $('#'+this.bstableId).offset().top;
            var b_height = document.documentElement.clientHeight;
            this.height = b_height-t_top-37+this.subHeight;
        },
        setSubHeight:function(height){
            this.subHeight = height;
        },
        /**
         * 设置在哪一列上面显示展开按钮,从0开始
         */
        setExpandColumn: function (expandColumn) {
            this.expandColumn = expandColumn;
        },
        /**
         * 设置记录返回的id值
         */
        setIdField: function (id) {
            this.id = id;
        },
        /**
         * 设置记录分级的父级字段
         */
        setParentIdField: function (parentId) {
            this.parentId = parentId;
        },
        /**
         * 设置根节点code值----可指定根节点，默认为null,"",0,"0"
         */
        setRootIdValue: function (rootIdValue) {
            this.rootIdValue = rootIdValue;
        },
        /**
         * 设置是否默认全部展开
         */
        setExpandAll: function (expandAll) {
            this.expandAll = expandAll;
        },
        /**
         * 是否显示刷新按钮
         * @param param
         */
        setShowRefresh: function (showRefresh) {
            this.showRefresh = showRefresh;
        },
        /**
         * 是否显示所有的列
         * @param param
         */
        setShowColumns: function (showColumns) {
            this.showColumns = showColumns;
        },
        /**
         * 设置默认第一级展开--expandAll为false时生效
         */
        setExpandFirst: function (expandFirst) {
            this.expandFirst = expandFirst;
        },
        /**
         * 设置表格高度
         */
        setHeight: function (height) {
            this.height = height;
        },
        /**
         * 设置ajax post请求时候附带的参数
         */
        set: function (key, value) {
            if (typeof key == "object") {
                for (var i in key) {
                    if (typeof i == "function")
                        continue;
                    this.data[i] = key[i];
                }
            } else {
                this.data[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
            }
            return this;
        },

        /**
         * 设置ajax post请求时候附带的参数
         */
        setData: function (data) {
            this.data = data;
            return this;
        },

        /**
         * 清空ajax post请求参数
         */
        clear: function () {
            this.data = {};
            return this;
        },

        /**
         * 刷新表格
         */
        refresh: function (parms) {
            if (typeof parms != "undefined") {
                this.data = parms.query;
                this.btInstance.bootstrapTreeTable('refresh', parms.query);// 为了兼容bootstrap-table的写法
            } else if(typeof this.data != "undefined"){
                this.btInstance.bootstrapTreeTable('refresh',this.data);
            } else{
                this.btInstance.bootstrapTreeTable('refresh');
            }
        }
    };

    window.BSTreeTable = BSTreeTable;

}());